package com.dap.cms.governance.service.impl;

import com.dap.cms.base.impl.BaseServiceImpl;
import com.dap.cms.governance.exception.ServiceException;
import com.dap.cms.governance.po.*;
import com.dap.cms.governance.service.*;
import com.dap.cms.governance.vo.ManagerInfoVo;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.util.CryptographyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_manager表对应的服务接口实现类
 * @author: Fesine
 * @createTime:2018/08/20
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/20
 */
@Service
public class CmsManagerServiceImpl extends BaseServiceImpl<CmsManagerPo>
        implements CmsManagerService {
    @Autowired
    private CmsUserService cmsUserService;
    @Autowired
    private CmsCompanyService cmsCompanyService;
    @Autowired
    private CmsSysService cmsSysService;
    @Autowired
    private CmsComponentManagerService cmsComponentManagerService;
    @Autowired
    private CmsComponentService cmsComponentService;

    @Override
    public CmsManagerPo login(String username, String password) {
        CmsManagerPo user = new CmsManagerPo();
        user.setUsername(username);
        user.setPassword(CryptographyUtil.md5(password, "cms"));
        return daoService.selectOne(user);
    }

    @Override
    public ManagerInfoVo getManagerInfo(CmsManagerPo cmsManagerPo) {
        cmsManagerPo = get(cmsManagerPo);
        ManagerInfoVo managerInfoVo = new ManagerInfoVo();
        BeanUtils.copyProperties(cmsManagerPo, managerInfoVo);
        //如果是开发人员需要绑定公司及系统信息
        if (cmsManagerPo.getManagerFlag() == 1 || cmsManagerPo.getManagerFlag() == 2) {
            //根据用户名查询公司及系统信息
            CmsUserPo cmsUserPo = new CmsUserPo();
            cmsUserPo.setName(cmsManagerPo.getUsername());
            cmsUserPo = cmsUserService.get(cmsUserPo);
            CmsCompanyPo cmsCompanyPo = new CmsCompanyPo();
            cmsCompanyPo.setId(cmsUserPo.getCompanyId());
            String companyName = cmsCompanyService.get(cmsCompanyPo).getCompanyName();
            CmsSysPo cmsSysPo = new CmsSysPo();
            cmsSysPo.setId(cmsUserPo.getSysId());
            cmsSysPo = cmsSysService.get(cmsSysPo);
            String sysName = cmsSysPo.getSysName();
            managerInfoVo.setCompanyName(companyName);
            managerInfoVo.setCompanyId(cmsSysPo.getCompanyId());
            managerInfoVo.setSysName(sysName);
            managerInfoVo.setSysId(cmsSysPo.getId());
        }
        //如果是组件接入人员或组件管理员需要绑定组件信息
        if (cmsManagerPo.getManagerFlag() == 1 || cmsManagerPo.getManagerFlag() == 3) {
            CmsComponentManagerPo cmsComponentManagerPo = new CmsComponentManagerPo();
            cmsComponentManagerPo.setManagerId(cmsManagerPo.getId());
            List<CmsComponentManagerPo> cmsComponentManagerPos = cmsComponentManagerService.listAll(cmsComponentManagerPo);
            List<String> componentNameArray = new ArrayList<>();
            List<String> componentIdArray = new ArrayList<>();
            for (CmsComponentManagerPo componentManagerPo : cmsComponentManagerPos) {
                componentIdArray.add(componentManagerPo.getComponentId() + "");
                CmsComponentPo cmsComponentPo = new CmsComponentPo();
                cmsComponentPo.setId(componentManagerPo.getComponentId());
                componentNameArray.add(cmsComponentService.get(cmsComponentPo).getComponentName());
            }
            String componentNames = String.join(",", componentNameArray);
            String componentIds = String.join(",", componentIdArray);
            managerInfoVo.setComponentIds(componentIds);
            managerInfoVo.setComponentNames(componentNames);
        }
        return managerInfoVo;
    }

    @Override
    public QueryResult<CmsManagerPo> selectManagerInfos(CmsManagerPo cmsManagerPo, Integer pageIndex, Integer pageSize) {

        return daoService.selectQueryResult(CmsManagerPo.class.getName() + ".selectManagerInfos", cmsManagerPo, pageIndex, pageSize);

    }

    @Rollback
    @Override
    public void deleteManager(CmsManagerPo cmsManagerPo) {
        cmsManagerPo = get(cmsManagerPo);
        CmsComponentManagerPo cmsComponentManagerPo = new CmsComponentManagerPo();
        cmsComponentManagerPo.setManagerId(cmsManagerPo.getId());
        try {
            cmsComponentManagerService.delete(cmsComponentManagerPo);
            delete(cmsManagerPo);
        } catch (Exception e) {
            throw new ServiceException(500, "删除失败");
        }
    }

    @Override
    public CmsManagerPo selectManagerByComponentId(Integer componentId) {
        Map<String,Object>map=new HashMap<>();
        map.put("componentId",componentId);
        return daoService.selectOne(CmsManagerPo.class.getName()+".selectManagerByComponentId",map);
    }
}

